package Dynamic_programming;

import java.util.*;

/**
 * @Author 万家欣
 * @Date 2022/4/15 8:46
 * @Version 1.0
 */
public class NestedInteger {
    int val;
    List<NestedInteger> integers;
    // Constructor initializes an empty nested list.
    public NestedInteger(){
            integers = new ArrayList<>();
    };

    // Constructor initializes a single integer.
    public NestedInteger(int value){
        this.val = value;
        integers = new ArrayList<>();
    };

    // @return true if this NestedInteger holds a single integer, rather than a nested list.
    public boolean isInteger(){
        return val == 0;
    };

    // @return the single integer that this NestedInteger holds, if it holds a single integer
    // Return null if this NestedInteger holds a nested list
    public Integer getInteger(){
        return val;
    };
    // Set this NestedInteger to hold a single integer.
    public void setInteger(int value){
        val = value;
    };
    // Set this NestedInteger to hold a nested list and adds a nested integer to it.
    public void add(NestedInteger ni){
        integers.add(ni);
    };
    // @return the nested list that this NestedInteger holds, if it holds a nested list
    // Return empty list if this NestedInteger holds a single integer
    public List<NestedInteger> getList(){
        return integers;
    };

    public int mostFrequentEven(int[] nums) {
        Map<Integer,Integer> map  = new HashMap<>();
        for (int x:nums) {
            if(x % 2 == 0){
                map.put(x,map.getOrDefault(x,0) + 1);
            }
        }
        int res = -1;
        int index = -1;
        for (Map.Entry<Integer,Integer> x:map.entrySet()) {
            if(x.getValue() > index){
                res = x.getKey();
                index = x.getValue();
            }else if(x.getValue() == index){
                if(res > x.getKey()){
                    res = x.getKey();
                }
            }
        }
        return res;
    }
}
